package com.ruoyi.gis.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.gis.domain.DatasetRasterTaskData;
import com.ruoyi.gis.domain.DatasetRasterTaskLine;
import com.ruoyi.gis.domain.DatasetRasterTaskNode;
import com.ruoyi.gis.service.IDatasetRasterTaskDataService;
import com.ruoyi.gis.service.IDatasetRasterTaskLineService;
import com.ruoyi.gis.service.IDatasetRasterTaskNodeService;
import com.ruoyi.gis.vo.AddRasterTaskCodeVo;
import com.ruoyi.gis.vo.AddRasterTaskFlowVo;
import com.ruoyi.gis.vo.AddRasterTaskVo;
import com.ruoyi.gis.vo.TaskFlowConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gis.domain.DatasetRasterTask;
import com.ruoyi.gis.service.IDatasetRasterTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 遥感监测任务Controller
 *
 * @author rww
 * @date 2024-09-20
 */
@RestController
@RequestMapping("/earthai/rasterTask")
public class DatasetRasterTaskController extends BaseController
{
    @Autowired
    private IDatasetRasterTaskService datasetRasterTaskService;
    @Autowired
    private IDatasetRasterTaskNodeService datasetRasterTaskNodeService;
    @Autowired
    private IDatasetRasterTaskLineService datasetRasterTaskLineService;
    @Autowired
    private IDatasetRasterTaskDataService datasetRasterTaskDataService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询遥感监测任务列表
     */
//    @PreAuthorize("@ss.hasPermi('earthai:rasterTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(DatasetRasterTask datasetRasterTask)
    {
        startPage();
        List<DatasetRasterTask> list = datasetRasterTaskService.selectDatasetRasterTaskList(datasetRasterTask);
        return getDataTable(list);
    }

    /**
     * 导出遥感监测任务列表
     */
//    @PreAuthorize("@ss.hasPermi('earthai:rasterTask:export')")
    @Log(title = "遥感监测任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DatasetRasterTask datasetRasterTask)
    {
        List<DatasetRasterTask> list = datasetRasterTaskService.selectDatasetRasterTaskList(datasetRasterTask);
        ExcelUtil<DatasetRasterTask> util = new ExcelUtil<DatasetRasterTask>(DatasetRasterTask.class);
        util.exportExcel(response, list, "遥感监测任务数据");
    }

    /**
     * 获取遥感监测任务详细信息
     */
//    @PreAuthorize("@ss.hasPermi('earthai:rasterTask:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        DatasetRasterTask task = datasetRasterTaskService.selectDatasetRasterTaskById(id);
        return success(task);
    }


    @GetMapping(value = "/getDetail/{id}")
    public AjaxResult getDetail(@PathVariable("id") Long id)
    {
        DatasetRasterTask task = datasetRasterTaskService.selectDatasetRasterTaskById(id);
        AddRasterTaskVo taskVo = new AddRasterTaskVo();
        taskVo.setTask(task);
        DatasetRasterTaskNode nodeQuery = new DatasetRasterTaskNode();
        nodeQuery.setTaskId(task.getId());
        List<DatasetRasterTaskNode> taskNodes = datasetRasterTaskNodeService.selectDatasetRasterTaskNodeList(nodeQuery);
        DatasetRasterTaskLine lineQuery = new DatasetRasterTaskLine();
        lineQuery.setTaskId(task.getId());
        List<DatasetRasterTaskLine> taskLines = datasetRasterTaskLineService.selectDatasetRasterTaskLineList(lineQuery);
        DatasetRasterTaskData dataQuery = new DatasetRasterTaskData();
        dataQuery.setTaskId(task.getId());
        List<DatasetRasterTaskData> taskData = datasetRasterTaskDataService.
                selectDatasetRasterTaskDataList(dataQuery);
        taskVo.setDatasets(taskData);
        TaskFlowConfig config = new TaskFlowConfig();
        config.setName(task.getFlowName());
        config.setLineList(taskLines);
        config.setNodeList(taskNodes);
        taskVo.setConfig(config);
        return success(taskVo);
    }

    /**
     * 新增遥感监测任务
     */
//    @PreAuthorize("@ss.hasPermi('earthai:rasterTask:add')")
    @Log(title = "遥感监测任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DatasetRasterTask datasetRasterTask)
    {
        return toAjax(datasetRasterTaskService.insertDatasetRasterTask(datasetRasterTask));
    }
    @PostMapping("saveTask")
    public AjaxResult saveTask(HttpServletRequest request, @RequestBody AddRasterTaskVo taskVo)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        return toAjax(datasetRasterTaskService.saveTask(loginUser,taskVo));
    }

    @PostMapping("saveTaskFlow")
    public AjaxResult saveTaskFlow(HttpServletRequest request, @RequestBody AddRasterTaskFlowVo taskVo)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        return toAjax(datasetRasterTaskService.saveTaskFlow(loginUser,taskVo));
    }
    @PostMapping("saveTaskCode")
    public AjaxResult saveTaskCode(HttpServletRequest request, @RequestBody AddRasterTaskCodeVo taskVo)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        return toAjax(datasetRasterTaskService.saveTaskCode(loginUser,taskVo));
    }


    /**
     * 修改遥感监测任务
     */
//    @PreAuthorize("@ss.hasPermi('earthai:rasterTask:edit')")
    @Log(title = "遥感监测任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DatasetRasterTask datasetRasterTask)
    {
        return toAjax(datasetRasterTaskService.updateDatasetRasterTask(datasetRasterTask));
    }

    /**
     * 删除遥感监测任务
     */
//    @PreAuthorize("@ss.hasPermi('earthai:rasterTask:remove')")
    @Log(title = "遥感监测任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(datasetRasterTaskService.deleteDatasetRasterTaskByIds(ids));
    }
}
